package cn.picclife.mwx.manager.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.sys.model.bo.MenuBo;
import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import cn.picclife.mwx.manager.sys.service.MenuMgMtService;
import cn.picclife.mwx.manager.util.RestUtils;

/**
 * 
 * @Title MenuMgMtController
 * @Author cc
 * @Description:菜单管理功能
 * @Param
 * @date 2016年12月9日 上午9:53:44
 */
@Controller
@RequestMapping(value = "/api")
public class MenuMgMtApiController /*extends ApiBaseController*/ {
	@Autowired
	private MenuMgMtService menuMgMtService;

	/**
	 * 菜单查询
	 * 
	 * @param requset
	 * @return
	 */
	@RequestMapping(value = "/menu/query_menu_page")
	@ResponseBody
	public List<MenuBo> queryMenu(HttpServletRequest requset) {
		List<MenuBo> menuquery = menuMgMtService.queryMenu();
		return menuquery;
	}

	/**
	 * 添加菜单信息
	 * 
	 * @param requset
	 * @param response
	 * @param menuBo
	 * @return
	 */
	@RequestMapping(value = "/menu/add_menu")
	@ResponseBody
	public BaseResult1 addMenu(HttpServletRequest request, HttpServletResponse response, MenuBo menuBo) {
		BaseResult1 result = new BaseResult1();
		if (StringUtils.isBlank(menuBo.getMenuName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("菜单名称不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(menuBo.getParentNo())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("父节点编码不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(menuBo.getMenuOrder())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("菜单顺序不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(menuBo.getMenuStatus())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("菜单状态不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(menuBo.getMenuType())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("菜单类型不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(menuBo.getMenuFlag())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("请选择菜单是否公开");
			RestUtils.error(response, result);
			return null;
		}
		MenuBo menu = menuMgMtService.queryCountMenu(menuBo);
		if (menu == null) {
			UserSessionBo userSessionBo = (UserSessionBo)request.getSession().getAttribute("user");
			String optNo = userSessionBo.getOptNo();
			menuBo.setCrtUser(optNo);
			int count = menuMgMtService.insertMenu(menuBo);
			if (count > 0) {
				result.setRet(BaseResult1.SUCCESS);
				result.setMsg("插入成功");
				return result;
			} else {
				result.setRet(BaseResult1.BIZ_ERROR);
				result.setMsg("插入失败");
				RestUtils.error(response, result);
				return null;
			}
		} else {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("该菜单编号已存在");
			RestUtils.error(response, result);
			return null;
		}
	}

	/**
	 * 查询修改菜单信息
	 * 
	 * @param requset
	 * @param menuBo
	 * @return
	 */
	@RequestMapping(value = "/menu/query_menu_info")
	@ResponseBody
	public MenuBo queryMenuInfo(HttpServletRequest requset, MenuBo menuBo) {
		MenuBo updateModel = menuMgMtService.queryUpdateMenu(menuBo);
		return updateModel;
	}

	/**
	 * 更新菜单信息
	 * 
	 * @param requset
	 * @param response
	 * @param menuBo
	 * @return
	 */
	@RequestMapping(value = "/menu/update_menu")
	@ResponseBody
	public BaseResult1 updateMenu(HttpServletRequest request, HttpServletResponse response, MenuBo menuBo) {
		BaseResult1 result = new BaseResult1();
		if (StringUtils.isBlank(menuBo.getMenuName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("菜单名称不能为空");
			RestUtils.error(response, result);
			return null;
		}

		if (StringUtils.isBlank(menuBo.getParentNo())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("父节点编码不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(menuBo.getMenuOrder())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("菜单顺序不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(menuBo.getMenuStatus())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("菜单状态不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(menuBo.getMenuType())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("菜单类型不能为空");
			RestUtils.error(response, result);
			return null;
		}

		int count = menuMgMtService.updateMenuMgMt(menuBo);
		if (count > 0) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("保存成功");
			return result;
		} else {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("保存失败");
			RestUtils.error(response, result);
			return null;
		}
	}

	/**
	 * 删除菜单信息
	 * 
	 * @param requset
	 * @param response
	 * @param menuBo
	 * @return
	 */
	@RequestMapping(value = "/menu/delete_menu")
	@ResponseBody
	public BaseResult1 deleteMenu(HttpServletRequest requset, HttpServletResponse response, String  menuNo) {
		BaseResult1 result = new BaseResult1();
		int count = menuMgMtService.deleteMenuMgMt(menuNo);
		if (count > 0) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("删除成功");
			return result;
		} else {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("删除失败");
			RestUtils.error(response, result);
			return null;
		}
	}
}
